part of 'api.dart';

extension ApiMine on _Api {
  ///获取充值记录
  ///[tranType] 2:VIP记录 3:金币记录
  Future<List<RecordModel>> geRechargeRecordList({
    required int tranType,
    required int page,
    int pageSize = 20,
  }) async {
    try {
      final response = await httpInstance.get<RecordModel>(
        url: 'tran/view/list',
        queryMap: {
          'tranType': tranType,
          'page': page,
          'pageSize': pageSize,
        },
        complete: RecordModel.fromJson,
      );
      return response ?? [];
    } catch (e) {
      return [];
    }
  }

  //用户推广记录
  Future<List<ShareRecordModel>?> getShareRecordList({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<ShareRecordModel>(
        url: 'user/getUserProcess',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: ShareRecordModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  Future<List<MineMessageModel>?> getMineMessageList({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<MineMessageModel>(
        url: 'information/user/notice',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: MineMessageModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //用户收藏视频
  Future<List<VideoBaseModel>?> getUserFavoritesList({
    required int page,
    required int pageSize,
    required int videoMark, //1-长视频 2-短视频
  }) async {
    try {
      final resp = await httpInstance.get<VideoBaseModel>(
        url: 'video/likeList',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
          'videoMark': videoMark,
        },
        complete: VideoBaseModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //用户收藏资源
  Future<List<ResourceModel>?> getUserFavoritesResourceList({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<ResourceModel>(
        url: 'resource/userLikeList',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: ResourceModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //用户观看视频
  Future<List<VideoBaseModel>?> getBrowseRecord({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<VideoBaseModel>(
        url: 'video/getBrowseRecord',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: VideoBaseModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //用户购买视频
  Future<List<VideoBaseModel>?> getUserPurVideo({
    required int page,
    required int pageSize,
    required int videoMark, //1-长视频 2-短视频
  }) async {
    try {
      final resp = await httpInstance.get<VideoBaseModel>(
        url: 'video/userPurVideo',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
          'videoMark': videoMark,
        },
        complete: VideoBaseModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //清理用户观看/收藏/购买/下载记录
  Future<bool> clearUserRecord({
    required int type,
    required List<int> videoIds,
  }) async {
    try {
      final _ = await httpInstance.post(
        url: 'video/clearUserRecord',
        body: {
          'type': type, //清理类型:1-观看2-收藏3-购买4-下载
          'videoIds': videoIds,
        },
      );
      return true;
    } catch (e) {
      return false;
    }
  }

  //删除资源
  Future<bool> clearUserResource({
    required List<int> resourceIds,
  }) async {
    try {
      final _ = await httpInstance.post(
        url: 'resource/delLike',
        body: {
          'resourceIds': resourceIds,
        },
      );
      return true;
    } catch (e) {
      return false;
    }
  }

  //获取分享链接
  Future<ShareRespModel?> getShareLink() async {
    final resp = await httpInstance.get<ShareRespModel>(
      url: 'user/shared/link',
      complete: ShareRespModel.fromJson,
    );
    return resp;
  }

  //在线客服
  Future<ServiceModel?> getOnlineService() async {
    try {
      final resp = await httpInstance.get<ServiceModel>(
        url: 'news/customer/sign',
        complete: ServiceModel.fromJson,
      );
      return resp;
    } catch (e) {
      return null;
    }
  }

  //获取支付链接
  Future<PayLinkModel?> getRechargeUrl(RechargeRequestModel req) async {
    try {
      final resp = await httpInstance.post<PayLinkModel>(
        url: 'rech/sumbit',
        body: req.toJson(),
        complete: PayLinkModel.fromJson,
      );
      return resp;
    } catch (e) {
      return null;
    }
  }

  // 获取支付链接 authored by dzw
  Future<BaseRespModel> buyToFetchResult(RechargeRequestModel req) {
    return ApiCode.warp(
      httpInstance.post(
        url: 'rech/sumbit',
        body: req.toJson(),
      ),
    );
  }

//我的发布--帖子
  Future<List<PublicationsPostsModel>?> getPublicationsPostsList({
    required int userId,
    required int status,
    required int page,
    int pageSize = 20,
  }) async {
    try {
      final response = await httpInstance.get(
        url: 'community/dynamic/person/list',
        queryMap: {
          'userId': userId,
          'status': status,
          'page': page,
          'pageSize': pageSize,
        },
        complete: PublicationsPostsModel.fromJson,
      );
      return response ?? [];
    } catch (e) {
      return null;
    }
  }

//我的发布--帖子-删除
//我的发布--斗阴
  Future<List<VideoBaseModel>?> getPublicationsVideosList({
    required int userId,
    required int videoStatus,
    required int videoMark,
    required int page,
    int pageSize = 20,
  }) async {
    try {
      final response = await httpInstance.get(
        url: 'video/queryPersonVideoByType',
        queryMap: {
          'userId': userId,
          'videoMark': videoMark,
          'videoStatus': videoStatus,
          'page': page,
          'pageSize': pageSize,
        },
        complete: VideoBaseModel.fromJson,
      );
      return response ?? [];
    } catch (e) {
      logger.d("==============");
      return null;
    }
  }

  ///我邀请的人
  Future<List<SpreadUserModel>?> fecthSpreadUserList({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<SpreadUserModel>(
        url: 'user/getUserProcess',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: SpreadUserModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  ///动态收益
  Future<List<ProfitDynamicItemModel>?> fecthSpreadDynamiProfitList({
    required int page,
    required int pageSize,
    required int incomeType,
  }) async {
    try {
      final resp = await httpInstance.get<ProfitDynamicItemModel>(
        url: 'user/income',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
          "incomeType": incomeType,
        },
        complete: ProfitDynamicItemModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  ///用户收益统计
  Future<ProfitTotalModel?> fecthSpreadTotalData() async {
    try {
      final resp = await httpInstance.get<ProfitTotalModel>(
        url: 'user/incomeStat',
        complete: ProfitTotalModel.fromJson,
      );
      return resp;
    } catch (e) {
      return null;
    }
  }

  ///拼团会员
  Future<List<GroupMembersModel>?> getGroupMembersList(
      {required int page}) async {
    try {
      final response = await httpInstance.get(
          url: 'group/groupList',
          queryMap: {
            'page': page,
            'pageSize': 20,
          },
          complete: GroupMembersModel.fromJson);
      return response ?? [];
    } catch (_) {
      return null;
    }
  }

  ///拼团记录
  Future<List<GroupMembersModel>?> getGroupMembersHistoryList(
      {required int page}) async {
    try {
      final response = await httpInstance.get(
          url: 'group/history',
          queryMap: {
            'page': page,
            'pageSize': 20,
          },
          complete: GroupMembersModel.fromJson);
      return response ?? [];
    } catch (_) {
      return null;
    }
  }

  ///会员卡和金币列表
  Future<VipGoldTypeModel?> getVipGoldCards() async {
    try {
      final response = await httpInstance.get(
        url: 'user/vip/card/list',
        complete: VipGoldTypeModel.fromJson,
      );
      return response;
    } catch (e) {
      return null;
    }
  }

  ///帖子收益详情
  Future<List<BuyDynamic>?> getBuyDynamicDetails(
      {required int page, required int dynamicId}) async {
    try {
      final response = await httpInstance.get(
        url: 'community/dynamic/user/purDetails',
        queryMap: {
          'page': page,
          'pageSize': 20,
          'dynamicId': dynamicId,
        },
        complete: BuyDynamic.fromJson,
      );
      return response ?? [];
    } catch (e) {
      return null;
    }
  }

  ///视频收益详情
  Future<List<BuyDynamic>?> getBuyVideoDetails(
      {required int page, required int videoId, required int videoMark}) async {
    try {
      final response = await httpInstance.get(
        url: 'video/getPurVideoRecordByVideoId',
        queryMap: {
          'page': page,
          'pageSize': 20,
          'videoId': videoId,
          'videoMark': videoMark,
        },
        complete: BuyDynamic.fromJson,
      );
      return response ?? [];
    } catch (e) {
      return null;
    }
  }

  //用户收藏的合集
  Future<List<BloggerVideoCollectionModel>?> getUserFavoritesCollectList({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<BloggerVideoCollectionModel>(
        url: 'bloggerCollection/userFavorites',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: BloggerVideoCollectionModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //删除审核未通过的视频
  Future<bool> delCheckVideo({
    required List<int> videoId,
  }) async {
    try {
      final _ = await httpInstance.post(
        url: 'video/delAuditFailVideo',
        body: {
          'videoId': videoId,
        },
      );
      return true;
    } catch (e) {
      return false;
    }
  }

  //删除审核未通过的G圈
  Future<bool> delCheckCommunity({
    required List<int> dynamicIds,
  }) async {
    try {
      final _ = await httpInstance.post(
        url: 'community/dynamic/del',
        body: {
          'dynamicIds': dynamicIds,
        },
      );
      return true;
    } catch (e) {
      return false;
    }
  }

  //G圈购买列表
  Future<List<CommunityDateModel>?> getBuyCommunityDataList({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<CommunityDateModel>(
        url: 'community/dynamic/user/purList',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: CommunityDateModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //G圈收藏列表
  Future<List<CommunityDateModel>?> getFavoriteCommunityDataList({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<CommunityDateModel>(
        url: 'community/dynamic/userFavorite',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: CommunityDateModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //我发布的G圈列表
  Future<List<CommunityModel>?> getMineReleaseCommunityDataList({
    required int page,
    required int pageSize,
    required int status, //1-审核中，2-审核通过，3-审核拒绝
  }) async {
    try {
      final resp = await httpInstance.get<CommunityModel>(
        url: 'community/dynamic/person/list',
        queryMap: {
          // 'status': status,
          'page': page,
          'pageSize': pageSize,
        },
        complete: CommunityModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  //粉丝团购买列表
  Future<List<FansClubModel>?> getBuyFansClubDataList({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<FansClubModel>(
        url: 'bloggerFansGroup/getMyJoinList',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: FansClubModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }

  ///获取提现配置
  Future<WithdrawConfigModel?> getWithdrawalConfig() async {
    try {
      List<WithdrawConfigModel>? response = await httpInstance.get(
        url: "wd/config",
        complete: WithdrawConfigModel.fromJson,
      );
      if (response == null || response.isEmpty) {
        return null;
      }
      return response.first;
    } catch (e) {
      logger.e(e);
      return null;
    }
  }

  ///申请提现
  ///[payType]提现方式 (0-余额; 1-支付宝; 2-微信; 3-云闪付)
  ///[purType]提现货币类型 (1-余额; 2-金币)
  Future<bool> applyWithdrawal({
    required String money,
    required String receiptName,
    required String accountNo,
    required int payType,
    required int purType,
  }) async {
    try {
      await httpInstance.post(
        url: "wd/apply",
        body: {
          "accountNo": accountNo,
          "money": money,
          "payType": payType,
          "purType": purType,
          "receiptName": receiptName
        },
      );
      return true;
    } catch (e) {
      return false;
    }
  }

  Future<bool> exchange({
    required int satisfyType,
  }) async {
    try {
      await httpInstance.post(
        url: "user/satisfy/exchange",
        body: {
          "satisfyType": satisfyType,
        },
      );
      return true;
    } catch (e) {
      return false;
    }
  }

  Future<List<ExchangeModel>?> getExchangeRecord({
    required int page,
    required int pageSize,
  }) async {
    try {
      final resp = await httpInstance.get<ExchangeModel>(
        url: 'user/satisfy/exRecord',
        queryMap: {
          'page': page,
          'pageSize': pageSize,
        },
        complete: ExchangeModel.fromJson,
      );
      return resp ?? [];
    } catch (e) {
      return null;
    }
  }
}
